<?php
/* 
* [文章管理模型]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-06-19 15:02:23
* @Last Modified time: 2014-06-27 14:51:56
*/
class ArticleModel extends Model{
	public $table = 'article';

	public $validate = array(
		array('title','nonull','文章标题不能为空',2,3)

		);
	/**
	 * [$auto 自动完成]
	 * @var array
	 */
	public $auto = array(
		//1.字段名，2.方法名，3.种类有可能是function,4.验证条件，5.验证时机
		array('sendtime','_sendtime','method',2,1),
		array('user_uid','_uid','method',2,1),
		array('attr','_attr','method',2,3),
		);

	
	public function _attr(){
		$attr = Q('post.attr');
		//处理文章属性
		if(is_array($attr)){
			$attr = implode(',', $attr);
			return $attr;
		}
	}
	public function _sendtime(){
		return time();
	}

	public function _uid(){
		return session('aid');
	}

	public function get_one_data($where=NULL,$field=NULL){
		return $this->where($where)->field($field)->find();
	}

	/**
	 * [add_data 添加文章]
	 */
	public function add_data(){

		//1.文章主表----------------------
		//处理缩略图
		if(!empty($_FILES['thumb']['name'])){
			//实例化上传类
			$upload = new Upload();
			$arr = $upload->upload();
			if(!$arr) {
				$this->error = $upload->error;
				return false;
			}

			$data = current($arr);
			// var_dump($data);die;
			$this->data['thumb'] = $data['path'];
		}

		// die;
		$aid = $this->add();
		//2.文章标签中间表------------------
		//获得分类id
		$cid = Q('post.category_cid',0,'intval');
		if(isset($_POST['tid']) && is_array($_POST['tid'])){
			foreach ($_POST['tid'] as $v) {
				$data = array(
					'article_aid'	=>	$aid,
					'tag_tid'		=>	(int)$v,
					'category_cid'	=>	$cid
				);
				//执行
				K('ArticleTag')->add_data($data);
			}
		}
		//3.文章附表
		$arcData = array(
			'article_aid'	=>	$aid,	
			'keywords'		=>	$_POST['keywords'],
			'description'	=>	$_POST['description'],
			'content'		=>	$_POST['content']
			);
		K('ArticleData')->add_data($arcData);
		return true;
		
	}


	/**
	 * [hava_data 判断分类下面是否有文章]
	 * @return [type] [description]
	 */
	public function cate_hava_arc($cid){
		return $this->where(array('category_cid'=>$cid))->getField('aid');
	}


	/**
	 * [del_back 删除或者还原]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function del_back($aid,$isRecycle){
		$this->where(array('aid'=>$aid))->save(array('is_recycle'=>$isRecycle));
	}

	/**
	 * [true_del 删除文章]
	 * @return [type] [description]
	 */
	public function true_del($aid){
		//1.文章主表
		$where = array('aid'=>$aid);
		//删除缩略图-------
		$thumbPath = $this->where($where)->getField('thumb');
		//组合完整路径
		$thumbFullPath = ROOT_PATH . $thumbPath;
		if(is_file($thumbFullPath)){
			//删除缩略图
			unlink($thumbFullPath);
			//获得源图路径
			$sourcePath = str_replace(C('THUMB_ENDFIX'), '', $thumbFullPath);
			//删除源图
			if(is_file($sourcePath)) unlink($sourcePath);
		} 
		//删除主表
		$this->where(array('aid'=>$aid))->delete();
		//2.文章标签中间表
		$where = array('article_aid'=>$aid);
		K('ArticleTag')->del_data($where);
		//3.文章附表
		K('ArticleData')->del_data($where);
		return true;
	}
	public function get_ar_db()
	{
		
	}
	public function get_ar_db_by_id($id)
	{
		return $this->where($id)->find();
	}
	public function get_all($where=null,$field=null)
	{
		return $this->where($where)->field($field)->all();
	}
}
?>
